import { successResponse, errorResponse } from "server/utils/response";
import { Projects } from "server/models/Project";

export default defineEventHandler(async (event) => {
  try {
    const query = getQuery(event);

    // 获取项目列表
    const result = await Projects.getProjects({
      status: "published",
      sortBy: "created_at",
      sortOrder: "desc",
    });

    // 按项目分类分组数据
    const groupedData: Record<string, any> = {};
    result.projects.forEach((project) => {
      const category = project.category;
      if (!groupedData[category]) {
        groupedData[category] = [];
      }
      groupedData[category].push(project);
    });

    return successResponse(groupedData, "获取项目列表成功");
  } catch (error) {
    console.error("获取项目列表失败:", error);
    return errorResponse(
      "获取项目列表失败",
      error instanceof Error ? error.message : "未知错误"
    );
  }
});
